package com.tianhua.javs.core.engine.handler;

import com.tianhua.javs.client.bean.JavsTranslateContextBean;
import com.tianhua.javs.client.bean.ScriptVarMappingBean;
import com.tianhua.javs.client.bean.TranslateResultBean;
import com.tianhua.javs.client.enums.SegmentSceneEnum;
import com.tianhua.javs.client.model.MethodModel;
import com.tianhua.javs.core.constant.TagConstants;
import com.tianhua.javs.core.engine.PipelineTranslator;
import com.tianhua.javs.core.parse.SegmentSpreadParseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Description:
 * date: 2022/2/24
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Service
@Slf4j
public class MethodHandler extends AbstractElementHandler{

    @Resource(name = "businessPipelineTranslator")
    private PipelineTranslator businessPipelineTranslator;

    @Autowired
    private SegmentSpreadParseService segmentSpreadParseService;


    /**
     * 处理静态方法
     * @param javsShell
     * @param keyWard
     * @param translateContextBean
     * @return
     */
    public TranslateResultBean handleTranslate(String javsShell, String keyWard, JavsTranslateContextBean translateContextBean, SegmentSceneEnum sceneEnum){
        if(sceneEnum.getCode().equals(SegmentSceneEnum.STATIC_METHOD.getCode())){
            return dealStaticMethod(javsShell, keyWard, translateContextBean);
        }
        return dealObjectMethod(javsShell, keyWard, translateContextBean);
    }


    /**
     * 处理静态方法
     * @param javsShell
     * @param keyWard
     * @param translateContextBean
     * @return
     */
    private TranslateResultBean dealStaticMethod(String javsShell, String keyWard, JavsTranslateContextBean translateContextBean){
        String businessSegment = businessPipelineTranslator.translateKeyWard(translateContextBean,javsShell,keyWard);
        ScriptVarMappingBean scriptVarMappingBean = translateContextBean.getCurrentScriptMethodBean().getLastRunTimeVar();
        if(scriptVarMappingBean != null){
            addRunTimePackage(translateContextBean, scriptVarMappingBean.getVarClassName());
            String assignSegment = buildAssignSegment(javsShell, scriptVarMappingBean.getVarClassName(),scriptVarMappingBean.getJavaCode(), businessSegment);
            return TranslateResultBean.getInstance(assignSegment, true);
        }
        MethodModel methodModel = searchMethod(keyWard, translateContextBean);
        String varClassName = "";
        if(methodModel != null){
            addRuntimeVarAssign(javsShell, methodModel.getReturnClass(), translateContextBean,false);
            varClassName = methodModel.getReturnClass();
        }else {
            log.warn("method not fund,javsShell = {},keyWard = {}",javsShell,keyWard);
        }
        String varCode = getVarCodeFromKeyWard(keyWard);
        String javaSegment = buildAssignSegment(javsShell, varClassName, varCode, businessSegment);
        return TranslateResultBean.getInstance(javaSegment, true);
    }
    /**
     * 处理普通对象方法调用
     * @param javsShell
     * @param keyWard
     * @param translateContextBean
     * @return
     */
    private TranslateResultBean dealObjectMethod(String javsShell, String keyWard, JavsTranslateContextBean translateContextBean){
        MethodModel methodModel = searchMethod(keyWard, translateContextBean);
        if(methodModel != null){
            addRuntimeVarAssign(javsShell, methodModel.getReturnClass(), translateContextBean, true);

            String javaSegment = segmentSpreadParseService.getBlankPrefix(javsShell) + methodModel.getReturnClass() + TagConstants.BLANK_ONE + javsShell  + TagConstants.ROW_END;
            return TranslateResultBean.getInstance(javaSegment, true);
        }
        return TranslateResultBean.getInstance(keyWard);
    }
}
